import { Controller } from "../../mvc/core/Controller";
import { INoviceGiftTipController } from "./interfaces/INoviceGiftTipController";
import { IView } from "../../mvc/interfaces/IView";
import { NoviceGiftTipModule } from "../../../modules/NoviceGiftTipModule";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NoviceGiftBagConfig } from "../resource/NoviceGiftBagConfig";
import { NoviceGiftSprite } from "../view/novice/NoviceGiftSprite";
import { UiFlyDispalyObjectUtil } from "../view/uiIconBtn/UiFlyDispalyObjectUtil";
import { TweenLite } from "../../../com/greensock/TweenLite";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { Global } from "../../../com/gengine/global/Global";
import { GameProxy } from "../mvc/GameProxy";
import { INoviceGiftTipModule } from "../../../modules/interfaces/INoviceGiftTipModule";
type int = number;
//class NoviceGiftTipController
    
    export  class NoviceGiftTipController extends Controller implements INoviceGiftTipController
    {
        constructor()
        {
            
            super();this._guildLevelArr = [];
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._noviceGiftTip == null) 
            {
                this._noviceGiftTip = new NoviceGiftTipModule();
            }
            return this._noviceGiftTip;
        }

        protected /* override */ initServer(): void
        {
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.NoviceGiftItemListShow, this.onNoviceGiftItemListShowHandler);
            Dispatcher.addEventListener(EventName.NoviceGiftItemListGain, this.onNoviceGiftItemListGainHandler);
            Dispatcher.addEventListener(EventName.BackPackReqData, this.onBackPackReqDataHandler);
            Dispatcher.addEventListener(EventName.NoviceGiftUseInBag, this.onNoviceGiftUseInBagHandler);
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            var loc3=0;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            if (this._guildLevelArr.length == 0) 
            {
                this._guildLevelArr = NoviceGiftBagConfig.instance.getGuildLevels();
            }
            var loc1=cache.role.entityInfo.level;
            var loc2=-1;
            var loc4=this._guildLevelArr.length;
            loc3 = 0;
            while (loc3 < loc4) 
            {
                loc2 = this._guildLevelArr.indexOf(loc1);
                if (loc2 != -1) 
                {
                    break;
                }
                ++loc3;
            }
            if (loc2 == -1) 
            {
                return;
            }
            this._lastLevel = loc1;
            loc3 = 0;
            while (loc3 <= loc4) 
            {
                if ((loc5 = NoviceGiftBagConfig.instance.getNoviceGiftBagInfoByLevel(this._guildLevelArr[loc3])) != null) 
                {
                    if ((loc6 = cache.pack.backPackCache.getItemByCode(loc5.itemCode)) != null) 
                    {
                        if (this._noviceGift == null) 
                        {
                            this._noviceGift = new NoviceGiftSprite();
                        }
                        this._uid = loc6.uid;
                        this._noviceGift.updateItem(loc6);
                        this._noviceGift.show();
                        this._noviceGift.x = 380;
                        this._noviceGift.y = 150;
                        this._noviceGift.mouseEnabled = false;
                        loc7 = UiFlyDispalyObjectUtil.instance.getPoint(this._noviceGift);
                        TweenLite.to(this._noviceGift, 2, {"x": loc7.x, "y": loc7.y, "onComplete": this.onFlyNoviceGiftEnd});
                        this._level = this._guildLevelArr[loc3];
                        break;
                    }
                }
                ++loc3;
            }
            return;
        }

        /* internal  */onFlyNoviceGiftEnd(): void
        {
            this._noviceGift.mouseEnabled = true;
            UiFlyDispalyObjectUtil.instance.resetPoints();
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer): void
        {
            if (this._noviceGift.visible) 
            {
                this._noviceGift.visible = false;
            }
            else 
            {
                this._noviceGift.visible = true;
            }
            return;
        }

        /* internal  */onTimerTypeCompleteHandler(arg1: SecTimer): void
        {
            this._secTimer.stop();
            this._noviceGift.visible = true;
            return;
        }

        /* internal  */onNoviceGiftItemListShowHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            view.show(Global.stage.stageWidth / 2 - 200, 150);
            this._noviceGiftTip.update(loc1, this._level);
            if (this._secTimer) 
            {
                this._secTimer.stop();
            }
            return;
        }

        /* internal  */onNoviceGiftItemListGainHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=NoviceGiftBagConfig.instance.getNoviceGiftBagInfoByLevel(this._level);
            if (loc1 != null) 
            {
                loc2 = cache.pack.backPackCache.getItemByCode(loc1.itemCode);
            }
            if (loc2 == null) 
            {
                return;
            }
            this._uid = loc2.uid;
            GameProxy.packProxy.useItem(loc2.uid, 1);
            this._isUseNovice = true;
            return;
        }

        /* internal  */onBackPackReqDataHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            if (this._isUseNovice || this._uid == loc1) 
            {
                if (this._noviceGift != null) 
                {
                    if (this._level >= this._lastLevel) 
                    {
                        UiFlyDispalyObjectUtil.instance.reducePoint(this._noviceGift);
                        this._noviceGift.hide();
                    }
                    this.updateNoviceGift();
                    this._isUseNovice = false;
                }
            }
            return;
        }

        /* internal  */updateNoviceGift(): void
        {
            var loc3=null;
            var loc1=this._guildLevelArr.indexOf(this._level);
            this._level = this._guildLevelArr[loc1 + 1];
            var loc2=NoviceGiftBagConfig.instance.getNoviceGiftBagInfoByLevel(this._level);
            if (loc2 != null) 
            {
                loc3 = new ItemData(loc2.itemCode);
                if (loc3 != null) 
                {
                    if (!this._noviceGift.isHide) 
                    {
                        this._noviceGift.updateItem(loc3);
                    }
                    if (this._noviceGiftTip != null) 
                    {
                        this._noviceGiftTip.update(loc3, this._level);
                    }
                }
            }
            if (this._noviceGiftTip != null) 
            {
                if (this._level > this._lastLevel || loc1 == (this._guildLevelArr.length - 1)) 
                {
                    this._noviceGiftTip.gainGiftSuccess(true);
                }
                else 
                {
                    this._noviceGiftTip.gainGiftSuccess(false);
                }
            }
            return;
        }

        /* internal  */onNoviceGiftUseInBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1 == null) 
            {
                return;
            }
            this._uid = loc1.uid;
            return;
        }

        private /* var */_noviceGiftTip: INoviceGiftTipModule;

        private /* var */_noviceGift: NoviceGiftSprite;

        private /* var */_lastLevel: int;

        private /* var */_level: int;

        private /* var */_secTimer: SecTimer;

        private /* var */_isUseNovice: Boolean=false;

        private /* var */_uid: string="";

        private /* var */_guildLevelArr: Array<any>;
    }
